import numpy as np
import sys

#已存在的用户信息
lst = np.load('user.npy', allow_pickle='TRUE').item()
# lst = {'root': {'passwd': '123456', 'balance': 3000}, 'admin': {'passwd': '654321', 'balance': 3100}}

#商品信息
# goodslist = {'ID0001': {'name': '苹果', 'price': 10}, 'ID0002': {'name': '巧克力', 'price': 5.5},
#              'ID0003': {'name': '橙子', 'price': 12}, 'ID0004': {'name': '柚子', 'price': 14},
#              'ID0005': {'name': '牛奶', 'price': 3.5}, 'ID0006': {'name': '酸奶', 'price': 6}}
goodslist = np.load('goods.npy', allow_pickle='TRUE').item()
# np.save('goods.npy', goodslist )


Shopping_list ={}
#购物清单
money = 0
#总金额
out_money = 0
#余额



#登录
def login():
    global username, passwd, out_money
    username = input("请输入用户名:")
    if username == 'exit':
        exit()
    else:
        passwd = input("请输入密码:")
        if username in lst.keys() and passwd == lst[username]['passwd']:
            print("登录成功")
            print(" 欢迎登进三乐购物系统 ".center(40, "-"))
            print(f"您的余额为：",lst[username]['balance'])
            out_money = lst[username]['balance']
            goods()
        else:
            print("用户名和密码错误请重新输入")
            login()



#注册
def regist():
    new_username = input("请输入用户名:")
    if new_username in lst.keys():
        print("用户名已被占用，请重新输入")
        regist()
    elif new_username == 'exit':
        exit()
    else:
        new_passwd = input("请输入密码:")
        if new_passwd == 'exit':
            exit()
        else:
            new_balance = int(input("请输入账户余额:"))
            if new_balance < 0:
                print("你输入的金额小于零元，请重新输入")
                regist()
            elif new_balance == 'exit':
                exit()
            else:
                print("注册成功，并返回首页登录")
                lst.update({new_username:{'passwd':new_passwd,'balance':new_balance}})
                np.save('user.npy', lst)
                login()

#退出
def exit():
    # 退出后,按照要求打印当前用户已购买商品和余额
    print("您已购商品清单如下：")
    print(Shopping_list)
    print(f"您的余额为：{out_money}")
    print("退出三乐")
    sys.exit(0)


#商品页面
def goods():
    while 1:
        print("温馨提示：输入'exit'退出三乐购物系统，输入'1'查看商品详情")
        print("输入'1'查看商品详情")
        print("输入'2'查看已购买信息")
        print("输入'shopping'开始购物")
        choice = input("请输入:")
        if choice == '1':
            print("#".center(50, "#"))
            for i,j in goodslist.items():
                print(f"{i}-->{j}")
        elif choice == '2':
            print("您已购商品清单如下：")
            print(Shopping_list)
        elif choice == 'shopping':
            shopping()
        elif choice == 'exit':
            exit()
        else:
            print("您输入的格式错误，请重新输入！")
            goods()

#购物
def shopping():
    global  Shopping_cart, out_money , money, lst1
    shopinput = input("请输入商品编号:")
    if shopinput == 'exit':
        exit()
    elif shopinput in goodslist.keys():
        numbers = int(input("请输入购买数量:"))
        payment = input("是否确认支付：")
        print("*确认*", end="---")
        print("*退出*")
        while payment == '确认':
            money += goodslist[shopinput]['price'] * numbers
            if money <= out_money:
                lst1 = int(lst[username]['balance'])
                out_money -= goodslist[shopinput]['price'] * numbers
                print(f"已成功支付，你的余额为{out_money}")
                Shopping_list.update({goodslist[shopinput]['name']:{'单价': goodslist[shopinput]['price'],'件数': numbers}})
                lst[username]['balance'] = out_money
                # np.save('user.npy', lst)
                shopping()
            elif int(out_money) < money:
                print("您的余额不足！")
                behavior = input("请输入：")
                print("*退出三乐购物系统*", end="---")
                print("*返回首页*")
                if behavior == 'exit':
                    exit()
                elif behavior == '返回首页':
                    home()
            else:
                print("输入格式错误")
        if payment == 'exit':
            exit()
        else:
            print("输入格式错误，请重新输入")
    else:
        print("您输入的商品信息不存在，请重新输入...")
        shopping()



# 首页
def home():


    print("#".center(45, "#"))
    print(" 三乐购物系统欢迎您 ".center(40, "^"))
    print(" 菜单 ".center(45, "-"))
    print("*登录*")
    print("*注册*")
    print("*退出*")
    print(" ".center(45, " "))
    choice = input("请输入您的选择：")
    print("温馨提示：输入'exit'退出三乐购物系统")
    if choice == '登录':
        login()
    elif choice == '注册':
        regist()
    elif choice == 'exit':
        exit()
    else:
        print("您输入的格式错误，请重新输入")
        home()
home()



